<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__dict">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-select v-model="dataForm.status" placeholder="商品状态" clearable>
            <el-option :value="1" label="上架"> </el-option>
            <el-option :value="0" label="下架"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="dataForm.playDate"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="游玩日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="dataForm.goodsName"
            placeholder="商品名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t("query") }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            v-if="$hasPermission('scenic:goods:save')"
            @click="addOrUpdateHandle()"
            >{{ $t("add") }}</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="danger"
            v-if="$hasPermission('scenic:goods:delete')"
            @click="deleteHandle(id)"
            >{{ $t("deleteBatch") }}</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @sort-change="dataListSortChangeHandle"
        style="width: 100%"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="goodsName"
          label="商品名称"
          :show-overflow-tooltip="true"
          header-align="center"
          min-width="150"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="initNum"
          label="数量"
          header-align="center"
          min-width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="discountPrice"
          label="优惠价"
          header-align="center"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="normalPrice"
          label="原价"
          header-align="center"
          min-width="150"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="playDate"
          label="游玩日期"
          header-align="center"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="playTime"
          label="游玩时间"
          header-align="center"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="status"
          label="上/下架状态"
          header-align="center"
          align="center"
          min-width="140px"
        >
          <template slot-scope="scope">
           <el-switch
              @change="(val) => changeStatus(scope.row.id, val,scope.row.playDate)"
              :value="scope.row.status"
              :active-value="0"
              :inactive-value="1"
              active-color="#ddd"
              inactive-color="#13ce66"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="coverImg"
          label="商品封面"
          min-width="150"
          header-align="center"
          align="center"
        >
          <template slot-scope="scope">
            <el-image
              :src="scope.row.coverImg"
              style="width: 100px; height: 100px"
              v-if="scope.row.coverImg"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          min-width="200"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              v-if="$hasPermission('scenic:goods:update')"
              @click="addOrUpdateHandle(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              v-if="$hasPermission('scenic:goods:info')"
              @click="lookDetail(scope.row.id)"
              >查看</el-button
            >
            <el-button
              v-if="
                scope.row.goodsType == 2 &&
                $hasPermission('scenic:goods:update')
              "
              type="text"
              size="small"
              @click="edit(scope.row.id, 1)"
              >发布特价</el-button
            >
            <el-button
              type="text"
              v-if="$hasPermission('scenic:goods:delete')"
              size="small"
              @click="deleteHandle(scope.row.id)"
              >{{ $t("delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        :goodsType="type"
        @refreshDataList="getDataList"
      ></add-or-update>
      <!-- 详情 -->
      <detail ref="lookDetail" v-if="lookDetailVisible"> </detail>
    </div>
  </el-card>
</template>

<script>
var that;
import mixinViewModule from "@/mixins/view-module";
import detail from "./detail";
import AddOrUpdate from "./goods-add-or-update2";
export default {
  mixins: [mixinViewModule],
  filters: {
    delStatus(val) {
      if (val) {
        let d = that.goodsType.filter((item) => item.value == val);
        if (d.length != 0) {
          return d[0].label;
        }
      }
    },
  },
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/scenic/goods/page",
        getDataListIsPage: true,
        deleteURL: "/scenic/goods",
        deleteIsBatch: true,
      },
      type: "",
      goodsType: [
        {
          label: "特价",
          value: 1,
        },
        {
          label: "包船",
          value: 2,
        },
        {
          label: "拼船",
          value: 3,
        },
      ],
      dataForm: {
        merchantId:
          this.$store.state.user.MerchantId ||
          localStorage.getItem("MerchantId"),
        goodsType: 1,
        playDate: "",
        status: "",
        goodsName: "",
        // name: '',
        // merchantType: '',
        // status: '',
        // activity: '',
        // islandId: '',
        // linkName: '',
        // phone: '',
      },
    };
  },
  components: {
    detail,
    AddOrUpdate,
  },
  created() {
    that = this;
  },
  methods: {
    GetDateStr(AddCount) {
      var dd = new Date();
      dd.setDate(dd.getDate() + AddCount); //获取AddDayCount天后的日期
      var y = dd.getFullYear();
      var m = dd.getMonth() + 1; //获取当前月份的日期
      var d = dd.getDate();
      m = m > 9 ? m : "0" + m;
      d = d > 9 ? d : "0" + d;
      return y + "-" + m + "-" + d;
    },
    changeStatus(id, status, playDate) {
      console.log(this.GetDateStr(2) )
      console.log(playDate)
      if (status&&this.GetDateStr(2) != playDate) {
        return this.$message.error("日期已过期");
      }
      this.$confirm(
        `确定${status ? "上架" : "下架"}？`,
        this.$t("prompt.title"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      )
        .then(() => {
          this.$http
            .put("/scenic/goods", {
              id: id,
              status: status,
            })
            .then((res) => {
              if (res.data.code !== 0) {
                return this.$message.error(res.data.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.getDataList();
                },
              });
            });
        })
        .catch(() => {
          console.log(1);
        });
    },
  },
};
</script>
